package com.briup.facade.web.controller;

import com.briup.common.util.PageResult;
import com.briup.common.web.Message;
import com.briup.common.web.MessageUtil;
import com.briup.logging.annotation.LoggingAccess;
import com.briup.operator.entity.Operator;
import com.briup.operator.entity.extend.OperatorEx;
import com.briup.operator.service.IOpeatorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@LoggingAccess("算子模块")
@RestController
@Api(tags = "算子模块")
public class OperatorController {
    @Autowired
    private IOpeatorService opeatorService;

    // 新增算子
    @LoggingAccess("新增算子")
    @ApiOperation(value = "新增算子", notes = "参数格式为json,model中有参数含义")
    @PostMapping("/operator/")
    public Message insertOperator(@RequestBody Operator operator) {
        opeatorService.insertOperator(operator);
        return MessageUtil.success("添加成功");
    }

    // 查看算子详情(以及目录)
    @LoggingAccess("查看算子详情")
    @ApiOperation(value = "查看算子详情", notes = "参数格式为result风格路径")
    @GetMapping("/operator/{id}")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "算子id", paramType = "path", dataType = "int", required = true)
    )
    public Message operatorInfo(@PathVariable("id") Integer id) {
        OperatorEx operator = opeatorService.findById(id);
        return MessageUtil.success(operator);
    }

    // 删除算子
    @LoggingAccess("删除算子")
    @ApiOperation(value = "删除算子", notes = "参数格式为result风格路径")
    @DeleteMapping("/operator/{id}")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "算子id", paramType = "path", dataType = "int", required = true)
    )
    public Message deleteOperator(@PathVariable("id") Integer id) {
        opeatorService.deleteOperator(id);
        return MessageUtil.success("删除成功");
    }

    // 条件并分页查看算子列表(此处条件为算子类型和分类id)
    @LoggingAccess("条件并分页查看算子列表 ")
    @ApiOperation(value = "条件并分页查看算子列表 ", notes = "参数格式为json+result风格,条件为算子类型和算子分类id")
    @PostMapping("/operator/{pageNum}/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", paramType = "path", dataType = "int", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", paramType = "path", dataType = "int", required = true)
    })
    public Message findOperatorByCon(
                                     @PathVariable("pageNum") Integer pageNum,
                                     @PathVariable("pageSize") Integer pageSize,
                                     @RequestBody Operator operator) {
        PageResult<OperatorEx> operatorList = opeatorService.findOperatorByCon(operator, pageNum, pageSize);
        return MessageUtil.success(operatorList);
    }

    // 根据用户id以及算子url获取算子信息 (不确定是否有该需求)
    @LoggingAccess("根据用户id以及算子名称获取算子 ")
    @ApiOperation(value = "根据用户id以及算子名称获取算子", notes = "参数格式为result风格")
    @GetMapping("/operator/{userId}/{operatorUrl}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "path", dataType = "int", required = false),
            @ApiImplicitParam(name = "operatorName", value = "算子名称", paramType = "path", dataType = "String", required = false)
    })
    public Message findByUserIdAndOperatorName( @PathVariable("userId") Integer userId,  @PathVariable("operatorName") String operatorName) {
        Operator operator = opeatorService.findByUserIdAndOperatorUrl(userId,operatorName);
        return MessageUtil.success(operator);
    }

}
